<template>

<div class="commodityContainer">
  <div v-if="isLoading" style="display: flex">
    <el-skeleton-item variant="image" style="width: 480px; height: 480px" />
    <el-skeleton style="width: 600px" />
    <br />
  </div>
  <div class="content" v-else >
<!--    商品图片信息-->
    <div class="commodityImg">
      <img :src="commodity.img"/>
    </div>
<!--    商品详细信息-->
    <div class="commodityInfo">
      <span style="font-size: 20px;font-weight: 400;line-height: 25px;display: block;">{{commodity.name}}</span>
      <span style="color: #f33;font-size: 14px;font-weight: 400;line-height: 20px;margin-top: 7px;display: block;margin-bottom: 10px">
        {{commodity.description}}
      </span>
      <div style="background: #f0f0f0;height: 5px;width: 678px;"></div>
<!--      灰色的div-->
      <div style="height: 90px;width: 678px;background-color:#f8f8f8;">
        <div style="padding-top: 15px">
          <span style="margin-left: 15px">价 &nbsp; 格</span>
          <strong style="line-height: 18px;font-size: 23px;color: red;margin-left: 10px">￥{{commodity.price}}</strong>
        </div>
        <div style="padding-top: 10px;margin-top: 5px">
          <span style="margin-left: 15px">促 &nbsp; 销</span>
          <strong style="line-height: 18px;font-size: 18px;margin-left: 20px">立减 50元</strong>
        </div>
      </div>
<!--      分类-->
      <div style="margin-top: 25px;padding-left: 10px;color: #3e3e3e">
        <span>分类：</span>
        <a style="margin-left: 15px;cursor:pointer;;display: inline;;width: 50px;border-radius: 4px;border: 1px solid #ddd;color: #333;height: 26px;line-height: 26px;padding: 0 20px;">
          {{commodity.categoryName}}
        </a>
      </div>
<!--      数量-->
      <div style="margin-top: 25px;padding-left: 10px;color: #3e3e3e">
        <span>数量：</span>
        <el-input-number v-model="count" :min="1" style="width: 100px;margin-left: 15px"/>
      </div>
      <div style="margin-top: 25px;padding-left: 10px;color: #3e3e3e">
        <span>积分：</span>
        <span style="margin-left: 25px;color: #3e3e3e;line-height: 10px">购买可获得:{{commodity.rewardPoint}}积分</span>
      </div>

<!--      按钮-->
      <div class="btnList">
        <div
            @click="buyHandler"
            style="border: 1px solid transparent;cursor: pointer;display: inline-block;outline: 0;padding: 0;text-align: center;white-space: nowrap;border-radius: 6px;font-size: 20px;font-weight: 400;line-height: 38px;width: 200px;background: #ffebe9;border-color: #f33;color: #f33;">
          立即购买
        </div>
        <div
            @click="addShoppingCart"
            style="margin-left: 20px;border: 1px solid transparent;cursor: pointer;display: inline-block;outline: 0;padding: 0;text-align: center;white-space: nowrap;border-radius: 6px;font-size: 20px;font-weight: 400;line-height: 38px;width: 200px;background: #f33;border-color: #ffebe9;color: white;">
          加入购物车
        </div>
      </div>
    </div>
  </div >
</div>
</template>

<script setup lang="ts">

import {onMounted, ref} from "vue";
import router from "../../router";
import {useRouter} from "vue-router";
import {ElMessage} from "element-plus";
import commodityAPI from "../../api/commodity";
import {commodityModel} from "../../model/commodityModel";
import {userStore} from "../../store/userStore";
import {loginStore} from "../../store/loginStore";

const userStoreImpl = userStore();
const loginStoreImpl = loginStore();

const isLoading = ref(true);

//商品id
const commodityId = ref();
//商品
const commodity = ref<commodityModel>(
    {img: "", categoryId: 0, descImg: "", description: "", id: 0, name: "", price: 0}
);
//商品购买数量
const count = ref(1);

//购买商品按钮
const buyHandler = () => {
  if (userStoreImpl.isLogin()) {
    //TODO 制作一个购买页面，要求购买页面时新弹出的网页

  } else {
    ElMessage({
      type: 'info',
      message: '请先登录'
    })
    loginStoreImpl.show();
  }
}

//添加购物车按钮
const addShoppingCart = () => {

}

onMounted(() => {
  //获取url的id
  commodityId.value = router.currentRoute.value.query.id
  //从服务器端获取商品的详细信息
  commodityAPI.getById(commodityId.value)
      .then(res => commodity.value = res.data.data.commodity)
      .finally(() => isLoading.value = false)
})

</script>

<style scoped>
.commodityContainer{
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  border-top: 1px solid;
  padding-top: 55px;

  .content{
    width: 1240px;
    height: 100%;
    display: flex;

    .commodityImg{
      width: 500px;
      height: 500px;
    }

    .commodityInfo{
      width: 740px;
      height: 500px;
    }

  }
}

.btnList{
  margin-top: 20px;
}
</style>
